package com.myapi.ModelPoker;

import java.io.Serializable;
import java.util.ArrayList;

import com.myapi.client.PlayerHandDAO;
import com.myapi.client.DAO.PlayerDAO;
import com.myapi.server.BDD;

public abstract class Player implements Serializable {
	private String name;
	private String id;  //id must be unique for each Player
	private double chips;
	private PlayerHand playerHand;
	private ArrayList<PlayerHand> playerHandList;
	  
	
	public Player(){
		playerHandList=new ArrayList<PlayerHand>();
	}
	public Player(String name, String id, double chips) {
		setName(name);
		setId(id);
		setChips(chips);
		playerHand = new PlayerHand(this);
	}
	
        public PotContribution bet(double amount, boolean forced) {
            double potCont = amount;
            boolean allIn = false;
            if(getChips() - amount <= 0) {
		potCont = getChips();
                allIn = true;
                playerHand.setAllIn(true);
            }
            removeChips(potCont);
            return new PotContribution(this, potCont, allIn, forced);
        }
        
        public void muckPlayerHand() {
            getPlayerHand().setMucked(true);            
        }
        
	public String getName() {
		return name;
	}
        
        private void setName(String name) {
            this.name = name;
        }
	
	public String getId() {
		return id;
	}
        
        private void setId(String id) {
            this.id = id;
        }
        
	
	public double getChips() {
		return chips;
	}
	
	private void setChips(double chips) {
		this.chips = chips;
	}
	
	private void removeChips(double chips) {
		double diff = getChips() - chips;
		if(diff < 0)
			diff = 0;  //cannot have negative chips
		setChips(diff);
	}
	
	public PlayerHand getPlayerHand() {
		return playerHand;
	}
	
	public void setPlayerHand(PlayerHand pH) {
		playerHand = pH;
	}
	/**
	 * The winner win the chips
	 * @param amount
	 */
    public void addChips(double amount){
    	chips=chips+amount;
    	playerHand.addChips(amount);
    	
    }
        //Players are equal if they have the same ID
        public boolean equals(Object o) {
            if(o instanceof Player) {
		Player player = (Player) o;
		return player.getId().equals(getId());
            }
            else {
		return false;
            }
	}
        
        public int hashCode() {
		return getId().hashCode();
	}
		public PlayerDAO getInfo() {
			PlayerDAO playerDAO=new PlayerDAO();
						playerDAO.setChips(this.getChips());
			playerDAO.setId(this.getId());
			playerDAO.setName(this.getName());
			playerDAO.setPlayerHand(this.playerHand.getInfo(playerDAO));
				

			return playerDAO;
		}
	
	
}
